import { request } from '../../utils/request'
import { timestampToTime } from '../../utils/util'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  createActivity() {
    wx.navigateTo({
      url: '/packagePlan/createEvent/createEvent'
    })
  },

  getDataList() {
    request({
      url: '/mini/api/mine/queryActivityList'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          dataList: resdata.data
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toItem(item) {
    wx.navigateTo({
      url:
        '/packagePlan/activityDetail/activityDetail?from=' +
        'my&id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toSignList(item) {
    request({
      url: '/mini/api/selectTeam/activityDetail',
      method: 'POST',
      data: {
        id: Number(
          item.currentTarget.dataset.item.id
        )
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        let timeList = []
        resdata.data.activitySessions.forEach(
          (item2) => {
            timeList.push({
              key: item2.id,
              label:
                timestampToTime(
                  new Date(
                    item2.activityStartTime.replace(
                      /-/g,
                      '/'
                    )
                  ),
                  'm+d+h+f'
                ) +
                ' - ' +
                timestampToTime(
                  new Date(
                    item2.activityEndTime.replace(
                      /-/g,
                      '/'
                    )
                  ),
                  'm+d+h+f'
                )
            })
          }
        )
        setTimeout(() => {
          wx.navigateTo({
            url:
              '/packageMy/signList/signList?from=' +
              'ACTIVITY&item=' +
              JSON.stringify(
                item.currentTarget.dataset.item
              ) +
              '&dateList=' +
              JSON.stringify(timeList)
          })
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDataList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
